<?php

namespace app\index\service;

use app\common\model\ToolModel;
use app\index\model\UserModel;
use app\index\model\EditorModel;

class EditorService
{
    /**
     * 编辑人员列表
     * @author LSH
     * @return array
     */
    public static function getEditorListLogic($param)
    {
        $datas = EditorModel::dbGetEditorList($param);
        $data = $datas['data'];
        $img_operator = array_column($data, 'img_operator'); 
        $edit_operator = array_column($data, 'edit_operator'); 
        $user_id = array_filter(array_merge($img_operator,$edit_operator));

        $user = UserModel::getUserById($user_id);
        foreach ($datas['data'] as &$val) {
            $val['img_operator_name'] = $user[$val['img_operator']]??'';
            $val['edit_operator_name'] = $user[$val['edit_operator']]??'';
        }

        return $datas;
    }

    /**
     * 编辑人员修改
     * @author LSH
     * @return array
     */
    public static function updateOperatorInfoLogic($param){
        if(empty($param['sku'])){
            return returnJson(0,'参数错误');
        }
        if(!checkSku($param['sku'])){
            return returnJson(0,'失败,请刷新页面重试!');
        }
        if(empty($param['edit']) || empty($param['img'])){
            return returnJson(0,'编辑人员 和 图片人员 不可为空');
        }

        return EditorModel::dbUpdateOperatorInfo($param);
    }

    /**
     * sku编辑人员导入
     * @author LSH
     * @return array
     */
    public static function uploadSkuEditorInfo($datas, $param)
    {
        $error = [];
        $img_name = array_column($datas, '2'); 
        $edit_name = array_column($datas, '1'); 
        $user_name = array_filter(array_merge($img_name,$edit_name));

        $user = UserModel::getUserByNike($user_name);
        $skus = ToolModel::select('skus', ['sku'=>['IN',array_column($datas, '0')],'status'=>1], 'sku,spu','','sku');
        foreach ($datas as $v) {
            $data = [];
            $sku = $v[0];
            $edit_operator = $user[$v[1]]??'';
            $img_operator = $user[$v[2]]??'';
            
            if (!isset($skus[$sku])) {
                $error[] = "SKU: {$sku} 不存在或已禁用!  ";
                continue;
            }

            if(empty($edit_operator) && empty($img_operator)){
                $error[] = "SKU: {$sku} 缺失人员信息!  ";
                continue;
            }

            $data['sku'] = $sku;
            $data['spu'] = $skus[$sku];
            !empty($edit_operator)&& $data['edit'] = $edit_operator;
            !empty($img_operator)&& $data['img'] = $img_operator;
            
            if(!EditorModel::dbUpdateOperatorInfo($data)){
                $error[] = "SKU: {$sku} 修改失败!  ";
                continue;
            }
        }
        
        //错误信息输出
        if (!empty($error)) {
            $msg=[];
            foreach ($error as $v3){
                $msg[]=['msg'=>$v3];
            }
            $code = (count($datas) == count($error))?2:4;
            unset($datas,$data,$error);
            return returnArr($code, '',$msg);
        }
        unset($datas,$data,$error);
        //成功
        return returnArr();
    }
}